home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / DINKDEMO / DINKCLAS / DLISTSTU.C < prev    next >
Text File  |  1992-07-08  |  2KB  |  135 lines

  1. /*
  2.     File:        DListStuff.c
  3.  
  4.     Written by:    Mark Gross
  5.  
  6.     Copyright:    ⌐ 1992 by Applied Technical Software, all rights reserved.
  7.     Use at your own risk.
  8.  
  9. */
  10.  
  11. #include "DListStuff.h"
  12.  
  13.  
  14.     
  15. DLink* DLink::Init(DLink *n, void *item)
  16. {
  17.     if (n)
  18.         fNext = n;
  19.     else
  20.         fNext = NULL;
  21.     
  22.     fItem = item;
  23.     return this;
  24. }// end of constuctor
  25.  
  26.  
  27. DLink*    DLink::GetNext(void)
  28. {
  29.     return    fNext;
  30. }
  31.  
  32. void*    DLink::GetItem(void)
  33. {
  34.     return    fItem;
  35. }
  36.  
  37. void    DLink::SetNext(DLink* aLink)
  38. {
  39.     fNext = aLink;
  40. }
  41.  
  42. void    DLink::SetItem(void* anItem)
  43. {
  44.     fItem = anItem;
  45. }
  46.  
  47. // end of DLink class definition
  48.  
  49.  
  50.     
  51. DList* DList::Init(void)
  52. {
  53.     fLink = NULL;
  54.     fNumItems = 0;
  55.     return this;
  56. }// end of constuctor
  57.  
  58.  
  59. void    DList::AddItem(void* item)
  60. {
  61.     fLink = (new DLink)->Init(fLink, item);
  62.     fNumItems++;
  63. }// end of function AddItem
  64.  
  65.  
  66.  
  67. Boolean    DList::ItemInList(void* item)
  68. {
  69.     DLink  *temp, *last;
  70.     
  71.     last = NULL;
  72.     
  73.     for(temp = fLink; temp!=NULL; temp = temp->GetNext() )
  74.     {    if(temp->GetItem() == item)
  75.             return true; 
  76.         else
  77.             last = temp;
  78.     }
  79.     return false; 
  80. }
  81.  
  82. Boolean    DList::RemoveItem(void* item)
  83. {
  84.     DLink  *temp, *last;
  85.     
  86.     last = NULL;
  87.     
  88.     for(temp = fLink; temp!=NULL; temp = temp->GetNext() )
  89.     {    if(temp->GetItem() == item)
  90.         {
  91.             if(last==NULL) 
  92.                 fLink = temp->GetNext();
  93.             else
  94.                 last->SetNext(temp->GetNext());
  95.             
  96.             delete temp;
  97.             fNumItems--;
  98.             return true; //item found and removed
  99.         }
  100.         else
  101.             last = temp;
  102.     }
  103.     return false; 
  104. }// end of function RemoveItem
  105.  
  106.  
  107.  
  108. int        DList::NumItems(void)
  109. {
  110.     return fNumItems;
  111. }
  112.  
  113.     
  114. DIterator* DIterator::Init(DList* list)
  115. {
  116.     fCurLink = list->fLink;
  117.     return this;
  118. }// end of constructor
  119.  
  120.  
  121. void*    DIterator::GetCurrentThenIncrement(void)
  122. {
  123.     DLink* link = fCurLink;
  124.     
  125.     if(fCurLink)
  126.     {
  127.         fCurLink = fCurLink->GetNext();
  128.         return (link->GetItem() );
  129.     }
  130.     else
  131.         return NULL;
  132. }// end of function Next
  133.  
  134.  
  135.